package com.estate.business.impl;

import java.util.HashMap;
import java.util.List;

import com.estate.business.UserInfoService;
import com.estate.dao.UserInfoDao;
import com.estate.utils.EncodeUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.UserInfoVo;

public class UserInfoServiceImpl implements UserInfoService {

	private UserInfoDao userInfoDao;
	
	public boolean addUserInfo(UserInfoVo userInfo) {
		boolean result = false;
		result = userInfoDao.addVo(userInfo);
		return result;
	}

	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public List<Vo> findAll() {
		// TODO Auto-generated method stub
		return userInfoDao.findAll();
	}

	public HashMap<String, Integer> findAllNameAndId() {
		// TODO Auto-generated method stub
		List<UserInfoVo> userInfoList=userInfoDao.findAllUser();
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		for(int i=0;i<userInfoList.size();i++){
			map.put(userInfoList.get(i).getName(), userInfoList.get(i).getId());
		}
		return map;
	}

	public List<Vo> getAllUserInfo() {
		List<Vo> userInfoList = userInfoDao.findAll();
		return userInfoList;
	}

	public Vo getUserInfo(int userInfoId) {
		Vo vo = userInfoDao.findByPK(userInfoId);
		return vo;
	}

	public UserInfoVo updateUserInfo(UserInfoVo userInfo) {
		int id = userInfo.getId();
		UserInfoVo userInfoForm = (UserInfoVo) userInfoDao.findByPK(id);
		userInfo.setName(userInfoForm.getName());
		userInfo.setLoginName(userInfoForm.getLoginName());
		userInfo.setRegistDate(userInfoForm.getRegistDate());
		userInfo.setRole(userInfoForm.getRole());
		userInfo.setPassword(userInfoForm.getPassword());
		
		boolean result = userInfoDao.updateVo(userInfo);
		userInfo = (UserInfoVo) userInfoDao.findByPK(id);
		return userInfo;
	}

	public boolean deleteUserInfo(UserInfoVo userInfo) {
		boolean result = userInfoDao.deleteVo(userInfo);
		return result;
	}

	public boolean updatePassword(UserInfoVo userInfo, String newPassword) {
		userInfo.setPassword(EncodeUtil.getMD5Code(newPassword));
		this.getUserInfoDao().updateVo(userInfo);
		return true;
	}

	public boolean deleteMultiUser(String userIdString) {
		if(userIdString!= null && !"".equals(userIdString) && !",".equals(userIdString)){
			String[] delArray = userIdString.split(",");
			UserInfoVo userInfo = null;
			for(int i = 0;i<delArray.length;i++){
				userInfo = new UserInfoVo();
				userInfo.setId(Integer.parseInt(delArray[i]));
				this.deleteUserInfo(userInfo);
			}
		}
		return true;
	}

	
}
